package com.imc.platform.base.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.LessonSchedule;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.category.service.ICategoryService;
import com.imc.platform.base.menu.pojo.Menu;
import com.imc.platform.base.order.pojo.Order;
import com.imc.platform.base.order.service.IPlatOrderService;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 类描述：订单控制类
 * 开发者：YT
 * 创建日期：2018/10/27 22:28
 */
@Slf4j
@Controller
@RequestMapping("base/order")
public class PlatOrderController {

    @Autowired
    IPlatOrderService orderService;

    @Autowired
    MysqlUtil mysqlUtil;

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    IOrgService iOrgService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * QY 2018/9/3 8:01
     * 方法描述：传参对象
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple {
        Map <String, String> parmsMap;
    }

    /*
     *  sjc 2018-8-17 10:01:50
     *  方法描述：公告页面跳转
     */
    @GetMapping("/orderQuery")
    public String orderQuery(String menuId, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute("currentUser");
        List<Menu> menu=categoryService.queryMenuByRoleIdAndMenuId(user.getRoleId(),menuId);
        model.addAttribute("menu",menu);
        model.addAttribute("menuId",menuId);
        return "base/order/order";
    }

    /*
     * YT  2018/9/14 13:49
     * 方法描述：查询分类列表
     */
    @GetMapping("/queryOrderList")
    @ResponseBody
    public ResultDTO queryOrderList(Principal principal, Pageable pageable,@RequestParam String tradeNo,@RequestParam String userId,@RequestParam String status,@RequestParam String prepayId,@RequestParam String tradeType,@RequestParam String startTime,@RequestParam String endTime) {
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String orgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map <String, String> mapP = new HashMap <>();
        mapP.put("tradeNo",tradeNo);
        mapP.put("userId",userId);
        mapP.put("status",status);
        mapP.put("prepayId",prepayId);
        mapP.put("tradeType",tradeType);
        mapP.put("startTime",startTime);
        mapP.put("endTime",endTime);

        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        if("admin".equals(user.getDuty())){
            mapP.put("dutyFlag", "0");
        }
        mapP.put("orgId", orgId);
        mapP.put("currentUserId", String.valueOf(user.getId()));
        PageInfo <Order> queryList = orderService.queryOrderByMap(pageable.getPageNumber(), pageable.getPageSize(), mapP);
        for (Order c : queryList.getList()) {
            c.setPageNumber(pageable.getPageNumber());
            c.setPageSize(pageable.getPageSize());
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /*
     * YT  2018/9/14 13:49
     * 方法描述：查询分类列表
     */
    @RequestMapping(value = "/queryOrderListS", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryOrderListS(Principal principal, Pageable pageable,@RequestParam String tradeNo,@RequestParam String userId,@RequestParam String status,@RequestParam String prepayId,@RequestParam String tradeType,@RequestParam String startTime,@RequestParam String endTime) {
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String orgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map <String, String> mapP = new HashMap <>();
        mapP.put("tradeNo",tradeNo);
        mapP.put("userId",userId);
        mapP.put("status",status);
        mapP.put("prepayId",prepayId);
        mapP.put("tradeType",tradeType);
        mapP.put("startTime",startTime);
        mapP.put("endTime",endTime);

        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        mapP.put("orgId", orgId);
        mapP.put("currentUserId", String.valueOf(user.getId()));
        PageInfo <Order> queryList = orderService.queryOrderByMap(pageable.getPageNumber(), pageable.getPageSize(), mapP);
        for (Order c : queryList.getList()) {
            c.setPageNumber(pageable.getPageNumber());
            c.setPageSize(pageable.getPageSize());
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /**
     * QY 2018/9/3 10:16
     * 方法描述：查询所有订单
     **/
    @RequestMapping(value = "/queryOrderByMap", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryOrderByMap(Principal principal, @RequestBody ReqTuple reqTuple) {
        String uname = principal.getName();
        User user = userMapper.queryRoleUserByLoginName(uname);
        String orgId = iOrgService.queryOrgIdByUserId(String.valueOf(user.getId()));
        Map <String, String> mapP = reqTuple.getParmsMap();
        //4是老师
        if("master".equals(user.getDuty())){
            mapP.put("dutyFlag", "1");
        }
        if("branch".equals(user.getDuty())){
            mapP.put("dutyFlag", "2");
        }
        if("teacher".equals(user.getDuty())){
            mapP.put("dutyFlag", "3");
        }
        mapP.put("orgId", orgId);
        mapP.put("currentUserId", String.valueOf(user.getId()));
        PageInfo <Order> queryList = orderService.queryOrderByMap(Integer.parseInt(reqTuple.getParmsMap().get("page")), Integer.parseInt(reqTuple.getParmsMap().get("size")), mapP);
        for (Order c : queryList.getList()) {
            c.setPageNumber(Integer.parseInt(reqTuple.getParmsMap().get("page")));
            c.setPageSize(Integer.parseInt(reqTuple.getParmsMap().get("size")));
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /**
     * QY 2018/10/08 23:49
     * 方法描述：跳转查看页面
     **/
    @RequestMapping(value = "/showSOrderPage", method = RequestMethod.GET)
    public String showSOrderPage(@RequestParam String orderId, @RequestParam String detailId, Model model) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("detailId", detailId);
        return "base/order/showOrder";
    }

    /**
     * QY 2018/9/3 17:08
     * 方法描述：根据订单ID，明细ID查询订单
     * 一个订单有多个订单明细
     **/
    @RequestMapping(value = "/queryOrderOrDetailById", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryOrderOrDetailById(@RequestParam String orderId, @RequestParam String detailId) {
        return ResultDTO.builder()
                .data(orderService.queryOrderOrDetailById(orderId, detailId))
                .build();
    }

    @GetMapping("/toLessonScheduler")
    public String toLessonScheduler(String detailId, String orderId,Model model) {
        model.addAttribute("detailId", detailId);
        model.addAttribute("orderId", orderId);
        return "base/order/showLessonSchedule";
    }

    @RequestMapping(value = "/queryLessonSchedule", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryLessonSchedule(Pageable pageable,@RequestParam String detailId) {
        Map <String, String> mapP = new HashMap <>();
        mapP.put("detailId",detailId);

        PageInfo <LessonSchedule> queryList = orderService.queryLessonSchedule(pageable.getPageNumber(), pageable.getPageSize(), mapP);
        for (LessonSchedule c : queryList.getList()) {
            c.setPageNumber(pageable.getPageNumber());
            c.setPageSize(pageable.getPageSize());
        }
        JSONObject map = new JSONObject();
        map.put("total", queryList.getTotal());
        map.put("rows", queryList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }
}